const { logger } = require('../utils');
const { knex, TABLE_NAME } = require('./db');

knex.schema.hasTable(TABLE_NAME.tenantSignRecord).then((exists) => {
  if (!exists) {
    return knex.schema.createTable(TABLE_NAME.tenantSignRecord, (t) => {
      t.charset('utf8');
      t.uuid('id').primary();
      t.string('contractId');
      t.string('tenantId');
      t.string('userId');
      t.foreign('contractId').references(`${TABLE_NAME.contract}.id`)
        .onDelete('SET NULL');
      t.foreign('tenantId').references(`${TABLE_NAME.tenant}.id`)
        .onDelete('CASCADE');
      t.foreign('userId').references(`${TABLE_NAME.user}.id`)
        .onDelete('SET NULL');
      t.timestamp('createdOn').defaultTo(knex.fn.now());
      t.string('tenantStatus');
      t.timestamp('endTime').defaultTo(knex.fn.now());
      t.timestamp('startTime').defaultTo(knex.fn.now());
      t.decimal('monthlyPay', 2);
    });
  }
})
  .catch((err) => {
    logger.error({
      message: err.message,
      stack: err.stack,
      Action: 'tenantSignRecord/createTable',
    });
    console.error(err);
  });

